package com.example.weather_plus.ui.weathergram.view;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.weather_plus.R;
import com.example.weather_plus.bmob.Entries;
import com.example.weather_plus.databinding.FragmentWGBinding;
import com.example.weather_plus.ui.weathergram.Items.Card;
import com.example.weather_plus.ui.weathergram.Items.SpacesItemDecoration;
import com.example.weather_plus.ui.weathergram.adapter.EntriesAdapter;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class EntriesFragment extends Fragment {

    private final AppCompatActivity activity_top;
    FragmentWGBinding entriesBinding; // entries页组件

    public EntriesAdapter entriesAdapter;


    public EntriesFragment(AppCompatActivity activity) {
        this.activity_top = activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        // 视图绑定：绑定Entries Frgament
        entriesBinding = FragmentWGBinding.inflate(getLayoutInflater());

        ArrayList<Card> cards = new ArrayList<>();

        // TODO:建立用户对笔记本的权限设置

        // 1. 查询Entries表中符合“笔记本=当前选择笔记本”的项
        BmobQuery<Entries> query = new BmobQuery<>();
        query.findObjects(new FindListener<Entries>() {
            @Override
            public void done(List<Entries> query_list, BmobException e) {
                if (e == null) {

                    // 向Arraylist<Card> card添加控件
                    // -1表示分节符；0代表entries cards
                    if (!query_list.isEmpty()) {

                        String section_date = null;     // 分节符日期


                        int i;
                        for (i = query_list.size() - 1; i >= 0; i--) {

                            Entries entries = query_list.get(i);

                            Card card = new Card(0, entries.getTitle(), entries.getText(), entries.getCreatedAt(), entries.getObjectId());

                            if (!card.getDate().equals(section_date)) {

                                // 若检测到下列卡片日期发生改变，则添加分节符卡片
                                section_date = card.getDate();
                                // card_id用以向适配器提供卡片类型信息，其等于-1时约定卡片类型为分节符
                                // 使用标题属性Card.title传递分节符内容
                                Card section_card = new Card(-1, section_date, null, entries.getCreatedAt(), entries.getObjectId());

                                // 将分节符加到新日期第一个卡片之前
                                cards.add(section_card);

                            }

                            cards.add(card);

                        }
                    }

                    // 设置适配器，以将卡片在RecycleView中展示
                    entriesAdapter = new EntriesAdapter(cards, getContext(), activity_top);

                    entriesBinding.entriesComponent.setAdapter(entriesAdapter); // 为绑定视图（循环视图）设置适配器

                    // 确定RecycleView的布局方式和滚动方向
                    // getContext()表示获取视图的上下文
                    entriesBinding.entriesComponent.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));

//                    Toast.makeText(activity_top, Integer.toString(i), Toast.LENGTH_SHORT).show();

                } else {
                    // TODO: 添加报错信息
                    Toast.makeText(activity_top, "查询日记本内容失败", Toast.LENGTH_LONG).show();
                }

            }
        });


        // 设置entries卡片上下间距
        RecyclerView recyclerView = entriesBinding.entriesComponent;
        int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.spacing); // 设置卡片间距
        recyclerView.addItemDecoration(new SpacesItemDecoration(spacingInPixels));

        return entriesBinding.getRoot();
    }
}